package cn.nange.cmfz.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.nange.cmfz.annotation.LogAnnotation;
import cn.nange.cmfz.dao.AdminDao;
import cn.nange.cmfz.entity.Admin;
import cn.nange.cmfz.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminDao adminDao;

    @RequestMapping("login")
    @LogAnnotation(content = "登录了", type = "管理员登录")
    public String login(String name, String pwd, String code) {

//        boolean verify = captcha.verify(code);
//        if (!verify) {
//            return "redirect:/login.jsp";
//        } else {
//            Admin login = adminService.login(name);
//            if (login != null&&login.getPwd().equals(pwd)){
//                session.setAttribute("admin",login);
//                return "redirect:/main.html";
//            }else {
//                return "redirect:/login.jsp";
//            }
//
//        }

        Session session = SecurityUtils.getSubject().getSession();
        ICaptcha captcha = (ICaptcha) session.getAttribute("captcha");
        System.out.println("code = " + code);
        System.out.println(captcha.getCode());
        String status = "";

//        将页面输入的账号密码封装到令牌中
        UsernamePasswordToken token = new UsernamePasswordToken(name, pwd);

//        获取主体，调用主体的 login 方法
        Subject subject = SecurityUtils.getSubject();

        try {
            subject.login(token);
            System.out.println("登录成功！");
            status = "redirect:/main.html";

//            Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
            Admin admin = adminService.login(name);

            session.setAttribute("admin", admin);

        } catch (Exception e) {
            System.out.println("登录失败！");
            status = "redirect:/login.jsp";
        }

        return status;

    }

    @RequestMapping("captcha")
    public void getCaptcha(HttpServletResponse resp, HttpSession session) throws IOException {
        ICaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(120, 38, 4, 20);
        session.setAttribute("captcha", circleCaptcha);
        circleCaptcha.write(resp.getOutputStream());
    }

    @RequestMapping("register")
    @LogAnnotation(content = "添加了新管理员", type = "添加")
    @ResponseBody
    public Map register(Admin a) {
        HashMap map = new HashMap();
        try {
            adminService.register(a);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }

    @RequestMapping("ban")
    @LogAnnotation(content = "冻结了管理员", type = "冻结管理员")
    @ResponseBody
    public Map ban(Integer id) {
        HashMap map = new HashMap();
        try {
            adminService.ban(id);
            map.put("isBan", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isBan", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("showOne")
    public Admin showOne(Integer id) {
        System.out.println(id);
        Admin admin = adminService.showOne(id);
        System.out.println("admin = " + admin);
        return admin;
    }

    @ResponseBody
    @RequestMapping("update")
    @LogAnnotation(content = "修改了管理员信息", type = "更新")
    public Map update(Admin a) {
        HashMap map = new HashMap();
        try {
            adminService.update(a);
            map.put("isUpdate", true);

        } catch (Exception e) {

            e.printStackTrace();

            map.put("isUpdate", false);
        }
        System.out.println("update:" + a);
        return map;
    }

    @ResponseBody
    @RequestMapping("delete")
    @LogAnnotation(content = "删除了管理员", type = "删除")
    public Map delete(int[] id) {
        HashMap map = new HashMap();
        try {
            for (int i = 0; i < id.length; i++) {
                adminService.delete(id[i]);
                System.out.println(id[i]);
                map.put("isDelete", true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("show")
    @LogAnnotation(content = "查询所有管理员", type = "查询全部")
    public Map show(Integer page, Integer limit) {

        PageInfo<Admin> show = adminService.showAll(page, limit);
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", show.getTotal());
        map.put("data", show.getList());
        return map;
    }

}
